﻿using KinonekoSoftware.Expressions;

namespace KinonekoSoftware.Studio.Components
{
    public sealed class NewXCoreWidgetViewModel : ObjectDialog<ExpressionNode>
    {
        public static Task<Result<ExpressionNode>> More(List<ExpressionNode> collection)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, collection);
            return Dialog<ExpressionNode>(new NewXCoreWidgetViewModel(), context);
        }
        
        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                var list = context.GetObject<List<ExpressionNode>>(ContextBase.Key_Value);
                Collection.AddMany(list, true);
            }
            return base.OnReceive(context);
        }
        
        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<NewXCoreWidgetViewModel>(string.Empty, x=> x.Selected is not null));
        }

        protected override ExpressionNode OnFinish(bool editing) => Selected;

        private ExpressionNode _selected;

        
        public ExpressionNode Selected
        {
            get => _selected;
            set => TryFinishAndSetValue(ref _selected, value);
        }

        public ObservableCollection<ExpressionNode> Collection { get; } = new ObservableCollection<ExpressionNode>();
    }
}